import locale

from collections import defaultdict

from django.contrib.auth.decorators import login_required
from django.contrib.messages import info, error
from django.core.urlresolvers import get_callable, reverse
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.template import RequestContext
from django.template.defaultfilters import slugify
from django.template.loader import get_template
from django.utils import simplejson
from django.utils.translation import ugettext as _
from django.views.decorators.cache import never_cache

from mezzanine.conf import settings
from mezzanine.utils.importing import import_dotted_path
from mezzanine.utils.views import render, set_cookie, paginate

from cartridge.shop import checkout
from cartridge.shop.forms import AddProductForm, DiscountForm, CartItemFormSet
from cartridge.shop.models import Product, ProductVariation, Order, OrderItem
from cartridge.shop.models import DiscountCode
from cartridge.shop.utils import recalculate_discount, sign

from db_conn_soundcloud import *

@login_required
def play_status(request, template="shop/plays_status.html"):
    cursor,conn = db_connect()
    user_email=request.user.email
    query_play_status="""
        SELECT  `id_play`,  `url`,  `login_request`,  `to_play`,  `plays_count`,  `errors_count`,  `last_plays_count`,  `last_plays_errors`,  `data_entrada`,  `update` FROM `soundcloud`.`plays` WHERE `login_request`='%s' LIMIT 1000;
    """ % (user_email)
    cursor.execute(query_play_status)
    response = cursor.fetchall()
    x = cursor.description
    resultsList = []   
    for r in response:
        i = 0
        d = {}
        while i < len(x):
            d[x[i][0]] = r[i]
            i = i+1
        resultsList.append(d)    
    context = {"play_status": resultsList}
    db_disconnect(cursor,conn)
    return render(request, template, context)